var BASE_URL = "http://localhost:8080/motd/resources/";

onload = function() {
    loadMessage();
    $("#submit").click(submitMessage);
};

function loadMessage() {
    
    var request = new XMLHttpRequest();
    request.open("GET", BASE_URL + "motd");
    request.onload = function() {
        if (request.status === 200) {
            $("#motd").text(request.responseText);
        } else {
            console.log("Error while loading message: " + request.status + " - " + request.responseText);
        }
    };
    request.setRequestHeader("Accept", "text/plain");
    request.send(null);
}

function submitMessage() {
    
    var newMessage = $("#newMessage").val();
    
    var request = new XMLHttpRequest();
    request.open("PUT", BASE_URL + "motd");
    request.onload = function() {
        if (request.status === 204) {
            loadMessage();
        } else {
             console.log("Error while sending message: " + request.status + " - " + request.responseText);
        }
    };
    request.setRequestHeader("Content-Type", "text/plain");
    request.send(newMessage);
}